package com.tanhua.mongo.api;

import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.model.mongo.Friend;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @Author: cl
 * @Date: 2022/5/7 - 05 - 07
 * @Description:
 */
@DubboService
public class FriendApiImpl implements FriendApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 添加好友(双向好友)
     *
     * @param userId
     * @param friendId
     */
    @Override
    public void contacts(Long userId, Long friendId) {

        //1、查询表中是否存在好友关系
        Query query = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        boolean b = mongoTemplate.exists(query, Friend.class);
        if (!b) {
            //2、创建好友对象
            Friend friend = new Friend();
            friend.setUserId(userId);
            friend.setFriendId(friendId);
            friend.setCreated(System.currentTimeMillis());
            //3、保存
            mongoTemplate.save(friend);
        }

        //4、双向好友操作

        Query query1 = Query.query(Criteria.where("friendId").is(userId).and("userId").is(friendId));
        if (!mongoTemplate.exists(query1, Friend.class)) {
            //2、创建好友对象
            Friend friend = new Friend();
            friend.setUserId(friendId);
            friend.setFriendId(userId);
            friend.setCreated(System.currentTimeMillis());
            //3、保存
            mongoTemplate.save(friend);
        }
    }

    /**
     * 查询所有好友关系
     *
     * @param userId
     * @return
     */
    @Override
    public List<Friend> findAllByUserId(Long userId) {
        //条件
        Query query = Query.query(Criteria.where("userId").is(userId));
        //查询
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        return friends;
    }

    /**
     * 分页查询好友关系
     *
     * @param page
     * @param pagesize
     * @param userId
     * @return
     */
    @Override
    public List<Friend> findByUserId(Integer page, Integer pagesize, Long userId) {
        //条件
        Query query = Query.query(Criteria.where("userId").is(userId))
                .skip((page - 1) * pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        //查询
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        return friends;
    }

    /**
     * 通过好友id删除好友
     *
     * @param userId
     * @param friendId
     * @return
     */
    @Override
    public boolean delete(Long userId, Long friendId) {
        try {
            //1、构造条件
            Query query1 = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
            //2、删除好友
            mongoTemplate.remove(query1, Friend.class);
            //3、删除双向好友
            Query query2 = Query.query(Criteria.where("userId").is(friendId).and("friendId").is(userId));
            mongoTemplate.remove(query2, Friend.class);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
